package com.example.raspberry_pi_demob.controller;

import com.example.raspberry_pi_demob.entity.AllData;
import com.example.raspberry_pi_demob.service.AllDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path="/allData")
public class AllDataController {
    //注入 alldata sevice
    @Autowired
    private AllDataService allDataService;

    /**
     * 查询所有信息列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<AllData> getAllDatas(){
        return allDataService.findAllDataList();
    }

    /**
     * 查询一个设备的信息
     * @param macAddress
     * @return
     */
    @RequestMapping(value = "/getByTimestamp/{timestamp}",method = RequestMethod.GET)
    public  AllData getDataByTimestamp(@PathVariable("timestamp") Integer timestamp){
        return allDataService.findAllDataByTimestamp(timestamp);
    }

    @RequestMapping(value = "/getByMacAddress/{macAddress}",method = RequestMethod.GET)
    public  AllData getDataByMacAddress(@PathVariable("macAddress") String macAddress){
        return allDataService.findAllDataByMacAddress(macAddress);
    }

    /**
     * 查询某一设备某个时间段的信息
     * @param startTime
     * @param endTime
     * @return
     */
    //根据书写的时间到service里进行比较
    @RequestMapping(value = "/getByTimeBucket/{startTime}/{endTime}",method = RequestMethod.GET)
    public  List<AllData> getDataByTime(@PathVariable("startTime") String startTime,@PathVariable("endTime")String endTime){
        return allDataService.findAllDataByTimeBucket(startTime,endTime);
    }
    /**
     * 删除一个信息
     * @param timestamp
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public  String delDataByTimeStamp(@PathVariable("timestamp") Integer timestamp){
        int flag = allDataService.delete(timestamp);
        return (flag == 1 ?  "delete success!" : "delete fail!");
    }

    /**
     * 修改信息
     * @param timestamp
     * @param numberOfSatellites
     * @param latitudeAndLongitude
     * @param battery
     * @param temperature
     * @param humidity
     * @param atmosphericPressure
     * @param macAddress
     * @return
     */
    @RequestMapping(value = "/update/{timestamp}",method = RequestMethod.PUT)
    public  String updateAllData(@PathVariable("timestamp")Integer timestamp , @RequestParam(value = "numberOfSatellites",required = true)Integer numberOfSatellites,
                              @RequestParam(value = "latitudeAndLongitude" ,required = true)String latitudeAndLongitude, @RequestParam(value = "battery",required = true)Integer battery,
                              @RequestParam(value = "temperature",required = true)Integer temperature, @RequestParam(value = "humidity",required = true)String humidity,
                              @RequestParam(value = "atmosphericPressure",required = true)String atmosphericPressure, @RequestParam(value = "macAddress",required = true)String macAddress,
                              @RequestParam(value = "datetime",required = true)String datetime,@RequestParam(value = "raspberrypiname",required = true)String raspberrypiname,
                              @RequestParam(value = "updateinterval" ,required = true)int updateinterval){
        AllData allData = new AllData();
        int t = allDataService.update(timestamp,allData);
        return (t==1 ? "update success!" : "update fail!");
    }

    /**
     * 新增信息
     * @param timestamp
     * @param numberOfSatellites
     * @param latitudeAndLongitude
     * @param battery
     * @param temperature
     * @param humidity
     * @param atmosphericPressure
     * @param macAddress
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public  String postAllData(@PathVariable("timestamp")Integer timestamp , @RequestParam(value = "numberOfSatellites",required = true)Integer numberOfSatellites,
                            @RequestParam(value = "latitudeAndLongitude" ,required = true)String latitudeAndLongitude, @RequestParam(value = "battery",required = true)Integer battery,
                            @RequestParam(value = "temperature",required = true)Integer temperature, @RequestParam(value = "humidity",required = true)String humidity,
                            @RequestParam(value = "atmosphericPressure",required = true)String atmosphericPressure, @RequestParam(value = "macAddress",required = true)String macAddress,
                            @RequestParam(value = "datetime",required = true)String datetime,@RequestParam(value = "raspberrypiname")String raspberrypiname,
                            @RequestParam(value = "updateinterval")int updateinterval){
        AllData allData = new AllData();
        int t= allDataService.save(allData);
        return (t==1 ? "insert success!" : "insert fail!");
    }

}
